<?php


namespace app\api\library\promotion\addPromotionUser;

use app\api\help\IdentityCard;
use think\facade\Db;

/**
 *
 * User: chenzheng
 * Date: 2023/5/20
 * Time: 10:45
 */
class V
{
    public static function run($param)
    {
        $userId   = _checkEmptyParam($param[ 'userId' ], '用户不能为空');
        $name     = _checkEmptyParam($param[ 'name' ], '姓名不能为空');
        $mobile   = _checkEmptyParam($param[ 'mobile' ], '手机号不能为空');
        $idNumber = _checkEmptyParam($param[ 'id_number' ], '身份证号不能为空');
        //检测手机号
        if ( !preg_match('/^1[3|4|5|8|7|9|6]\d{9}$/', $mobile)) {
            _outPut(204, [], '手机号码格式不正确');
        }
        //校验参数
        if (false === IdentityCard::isValid($idNumber)) {
            _outPut(204, [], '身份证格式错误');
        }
        $promotion = Db::name('mall_promotion')->where('user_id', $userId)->find();
        if ( !empty($promotion)) {
            if ($promotion[ 'status' ] == 0) {
                _outPut(204, [], '申请中');
            } else {
                _outPut(204, [], '您已经完成推广人申请');
            }
        }
        $addData = [
            'user_id'     => $userId,
            'name'        => $name,
            'mobile'      => $mobile,
            'id_number'   => $idNumber,
            'create_time' => time(),
            'update_time' => time(),
        ];
        if (false === Db::name('mall_promotion')->insertGetId($addData)) {
            _outPut(204, [], '申请失败');
        }
        
        _outPut(200, [], '申请成功');
    }
}